package org.ccunix.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccunix.eshop.domain.dto.CartSelectedMerVO;
import org.ccunix.eshop.domain.dto.CartVO;
import org.ccunix.eshop.domain.dto.MemberVO;
import org.ccunix.eshop.domain.dto.OrdersVO;
import org.ccunix.eshop.service.ShopCarServiceIface;
import org.ccunix.eshop.service.impl.ShopCarServiceImpl;
import org.ccunix.eshop.utils.AjaxResult;
import org.ccunix.eshop.utils.SessionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/auth/shopCar")
public class ShopCarController extends HttpServlet {
    ShopCarServiceIface shopCarService = new ShopCarServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("m");
        if ("list".equals(method)){
            list(request,response);
        }else if ("add".equals(method)){
            add(request,response);
        }else if ("delete".equals(method)){
            delete(request,response);
        }else if ("clear".equals(method)){
            clear(request,response);
        }else if ("updateNum".equals(method)){
            updateNum(request,response);
        }else if ("submit".equals(method)){
            submitShopCar(request,response);
        }
    }

    //提交购物车
    private void submitShopCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        CartVO cartVO = SessionUtil.getShopCarInfo(request);
        MemberVO memberVO = SessionUtil.getLoginMemberInfo(request);
        OrdersVO ordersVO = shopCarService.submitShopCar(cartVO.getId(),memberVO.getId());
        if (ordersVO!=null){
            AjaxResult result = AjaxResult.success("订单生成成功");
            //需要重新绑定购物车
            //创建购物车信息 获得
            CartVO cartVO2 = shopCarService.getShopCart(memberVO.getId());
            result.put("data",ordersVO);
            HttpSession session = request.getSession();
            session.setAttribute("shopCart",cartVO2);
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResult.error("订单生成失败")));
        }
    }

    //修改商品数量
    private void updateNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        String num = request.getParameter("num");
        CartVO cartVO = SessionUtil.getShopCarInfo(request);
        boolean b = shopCarService.updateCartSelectedMerNumById(id,Integer.parseInt(num),cartVO.getId());
        if (b){
            AjaxResult result = AjaxResult.success("购物车商品数量修改成功");
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResult.error("购物车商品数量修改失败")));
        }
    }

    //清空购物车
    private void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        CartVO cartVO = SessionUtil.getShopCarInfo(request);
        boolean b = shopCarService.clearCartSelectedMerByCart(cartVO.getId());
        if (b){
            AjaxResult result = AjaxResult.success("购物车清空商品成功");
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResult.error("购物车清空商失败")));
        }
    }

    //删除商品项
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        CartVO cartVO = SessionUtil.getShopCarInfo(request);
        boolean b = shopCarService.deleteCartSelectedMerById(id,cartVO.getId());
        if (b){
            AjaxResult result = AjaxResult.success("购物车删除商品成功，id="+id);
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResult.error("购物车删除商品失败")));
        }
    }

    //商品添加购物车
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String merId = request.getParameter("merId");
        CartVO cartVO = SessionUtil.getShopCarInfo(request);
        MemberVO memberVO = SessionUtil.getLoginMemberInfo(request);
        boolean b = shopCarService.addShopCar(cartVO.getId(),merId,memberVO.getFavourable());
        if (b){
            AjaxResult result = AjaxResult.success("购物车添加商品成功，id="+merId);
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResult.error("添加购物车失败")));
        }
    }

    //购物车列表
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        AjaxResult result = AjaxResult.success("购物车列表获取成功");
        //获得登录信息
        MemberVO memberVO = SessionUtil.getLoginMemberInfo(request);
        //获得购物车信息
        CartVO cartVO = SessionUtil.getShopCarInfo(request);
        List<CartSelectedMerVO> cartSelectedMerList = shopCarService.getCartSelectedMerListByCar(cartVO.getId());//根据购物车去查询购物详情
        result.put("data",cartSelectedMerList);
        response.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
